package com.dai.designpattern.prototype.practice.context;


import com.dai.designpattern.prototype.practice.AbstractScene;
import com.dai.designpattern.prototype.practice.factory.SceneFactory;

import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author daijiyong
 */
public class SceneContext {
    private final static Map<String, AbstractScene> CONTEXT = new ConcurrentHashMap<>();
    private final static ArrayList<AbstractScene> SCENES = new ArrayList<>();

    static {
        // 初始化场景集合
        SCENES.add(SceneFactory.createNumberReverseQueryScene());
        SCENES.add(SceneFactory.createMoveCarScene());
        SCENES.add(SceneFactory.createAreaCodeReverseQueryScene());
    }

    public static AbstractScene currentScene(String queryId, String userInput) {
        if (getContext().get(queryId) == null && SCENES.size() > 0) {
            for (AbstractScene as : SCENES) {
                if (as.match(userInput)) {
                    CONTEXT.put(queryId, as.clone());
                    System.out.printf("用户%s匹配到场景：%s\n", queryId, as.sceneNameCode.getName());
                    return getContext().get(queryId);
                }
            }
        }
        return getContext().get(queryId);
    }

    public static Map<String, AbstractScene> getContext() {
        return CONTEXT;
    }
}
